package city.spring.modules.oldman.controller;

import city.spring.modules.oldman.entity.OldmanLeaveApplyEntity;
import city.spring.modules.oldman.service.OldmanLeaveApplyService;
import city.spring.modules.oldman.vo.OldmanLeaveApplyEntityVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.NonNull;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * Controller：退住申请
 *
 * @author pxm
 * @date 2020-09-30 09:51:23
 */
@RestController
@RequestMapping("/oldman_leave_apply")
public class OldmanLeaveApplyController extends ApiController {
    private final OldmanLeaveApplyService oldmanLeaveApplyService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanLeaveApplyEntity, ?>> entityOrderFields;

    public OldmanLeaveApplyController(OldmanLeaveApplyService oldmanLeaveApplyService) {
        this.oldmanLeaveApplyService = oldmanLeaveApplyService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanLeaveApplyEntity::getId);
        entityOrderFields.add(OldmanLeaveApplyEntity::getRegistrant);
        entityOrderFields.add(OldmanLeaveApplyEntity::getOldman);
        entityOrderFields.add(OldmanLeaveApplyEntity::getApplyDate);
        entityOrderFields.add(OldmanLeaveApplyEntity::getLeaveDate);
        entityOrderFields.add(OldmanLeaveApplyEntity::getReason);
        entityOrderFields.add(OldmanLeaveApplyEntity::getRefundAccoun);
        entityOrderFields.add(OldmanLeaveApplyEntity::getGmtCreate);
        entityOrderFields.add(OldmanLeaveApplyEntity::getGmtModified);
        entityOrderFields.add(OldmanLeaveApplyEntity::getHandleStatus);
        entityOrderFields.add(OldmanLeaveApplyEntity::getLeaveForms);
        entityOrderFields.add(OldmanLeaveApplyEntity::getCheckInTime);
    }

    /**
     * 获取全部的 <strong>退住申请</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, OldmanLeaveApplyEntity entity) {
        LambdaQueryChainWrapper<OldmanLeaveApplyEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>退住申请</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, OldmanLeaveApplyEntityVO entity) {
        QueryWrapper<? extends OldmanLeaveApplyEntity> queryWrapper = buildQuery(entity);
        return success(oldmanLeaveApplyService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    /**
     * 获取可以使用的用户列表（前端选择用户时使用的用户列表）
     *
     * @param pageable 分页信息
     */
    @GetMapping("selectApplyPerson")
    public Object selectApplyPerson(@PageableDefault Pageable pageable, OldmanLeaveApplyEntityVO entity) {
        QueryWrapper<? extends OldmanLeaveApplyEntity> queryWrapper = buildQuery(entity);
        queryWrapper.eq("handle_status", 0);
        return success(oldmanLeaveApplyService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    /**
     * 获取一个 <strong>退住申请</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        QueryWrapper<OldmanLeaveApplyEntityVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        List<OldmanLeaveApplyEntityVO> records = oldmanLeaveApplyService.listPage(new Page<>(), wrapper).getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
    }

    /**
     * 添加一个 <strong>退住申请</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanLeaveApplyEntity entity, @NonNull Principal user) {
        oldmanLeaveApplyService.saveOldmanLeaveApply(entity, user);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>退住申请</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanLeaveApplyEntity entity) {
        entity.setId(id);
        oldmanLeaveApplyService.updateOldmanLeaveApply(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>退住申请</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanLeaveApplyService.deleteOldmanLeaveApply(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>退住申请</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanLeaveApplyService.deleteOldmanLeaveApply(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanLeaveApplyEntity> buildLambdaQuery(OldmanLeaveApplyEntity entity) {
        LambdaQueryChainWrapper<OldmanLeaveApplyEntity> lambdaQuery = oldmanLeaveApplyService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanLeaveApplyEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRegistrant()), OldmanLeaveApplyEntity::getRegistrant, entity.getRegistrant());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldman()), OldmanLeaveApplyEntity::getOldman, entity.getOldman());
        lambdaQuery.eq(entity.getApplyDate() != null, OldmanLeaveApplyEntity::getApplyDate, entity.getApplyDate());
        lambdaQuery.eq(entity.getLeaveDate() != null, OldmanLeaveApplyEntity::getLeaveDate, entity.getLeaveDate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getReason()), OldmanLeaveApplyEntity::getReason, entity.getReason());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRefundAccoun()), OldmanLeaveApplyEntity::getRefundAccoun, entity.getRefundAccoun());
        lambdaQuery.eq(entity.getGmtCreate() != null, OldmanLeaveApplyEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, OldmanLeaveApplyEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<? extends OldmanLeaveApplyEntity> buildQuery(OldmanLeaveApplyEntityVO entity) {
        QueryWrapper<? extends OldmanLeaveApplyEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getRegistrant()), "t1.registrant", entity.getRegistrant());
        wrapper.like(StringUtils.isNotBlank(entity.getRegistrantName()), "t2.nickname", entity.getRegistrantName());
        wrapper.like(StringUtils.isNotBlank(entity.getOldman()), "t1.oldman_id", entity.getOldman());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanName()), "t3.oldman_name", entity.getOldmanName());
        wrapper.like(StringUtils.isNotBlank(entity.getBedName()), "t6.name", entity.getBedName());
        wrapper.eq(entity.getApplyDate() != null, "t1.apply_date", entity.getApplyDate());
        wrapper.eq(entity.getLeaveDate() != null, "t1.leave_date", entity.getLeaveDate());
        wrapper.like(StringUtils.isNotBlank(entity.getReason()), "t1.reason", entity.getReason());
        wrapper.like(StringUtils.isNotBlank(entity.getRefundAccoun()), "t1.refund_accoun", entity.getRefundAccoun());
        wrapper.eq(entity.getGmtCreate() != null, "t1.gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "t1.gmt_modified", entity.getGmtModified());
        wrapper.eq(entity.getHandleStatus() != null, "t1.handle_status", entity.getHandleStatus());
        return wrapper;
    }

}
